1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gtk.IMContext;
26 
27 private import gdk.Device;
28 private import gdk.Event;
29 private import gdk.Surface;
30 private import glib.Str;
31 private import glib.c.functions;
32 private import gobject.ObjectG;
33 private import gobject.Signals;
34 private import gtk.Widget;
35 private import gtk.c.functions;
36 public  import gtk.c.types;
37 private import pango.PgAttributeList;
38 private import std.algorithm;
39 
40 
41 /**
42  * `GtkIMContext` defines the interface for GTK input methods.
43  * 
44  * `GtkIMContext` is used by GTK text input widgets like `GtkText`
45  * to map from key events to Unicode character strings.
46  * 
47  * An input method may consume multiple key events in sequence before finally
48  * outputting the composed result. This is called *preediting*, and an input
49  * method may provide feedback about this process by displaying the intermediate
50  * composition states as preedit text. To do so, the `GtkIMContext` will emit
51  * [signal@Gtk.IMContext::preedit-start], [signal@Gtk.IMContext::preedit-changed]
52  * and [signal@Gtk.IMContext::preedit-end] signals.
53  * 
54  * For instance, the built-in GTK input method [class@Gtk.IMContextSimple]
55  * implements the input of arbitrary Unicode code points by holding down the
56  * <kbd>Control</kbd> and <kbd>Shift</kbd> keys and then typing <kbd>u</kbd>
57  * followed by the hexadecimal digits of the code point. When releasing the
58  * <kbd>Control</kbd> and <kbd>Shift</kbd> keys, preediting ends and the
59  * character is inserted as text. For example,
60  * 
61  * Ctrl+Shift+u 2 0 A C
62  * 
63  * results in the € sign.
64  * 
65  * Additional input methods can be made available for use by GTK widgets as
66  * loadable modules. An input method module is a small shared library which
67  * provides a `GIOExtension` for the extension point named "gtk-im-module".
68  * 
69  * To connect a widget to the users preferred input method, you should use
70  * [class@Gtk.IMMulticontext].
71  */
72 public class IMContext : ObjectG
73 {
74 	/** the main Gtk struct */
75 	protected GtkIMContext* gtkIMContext;
76 
77 	/** Get the main Gtk struct */
78 	public GtkIMContext* getIMContextStruct(bool transferOwnership = false)
79 	{
80 		if (transferOwnership)
81 			ownedRef = false;
82 		return gtkIMContext;
83 	}
84 
85 	/** the main Gtk struct as a void* */
86 	protected override void* getStruct()
87 	{
88 		return cast(void*)gtkIMContext;
89 	}
90 
91 	/**
92 	 * Sets our main struct and passes it to the parent class.
93 	 */
94 	public this (GtkIMContext* gtkIMContext, bool ownedRef = false)
95 	{
96 		this.gtkIMContext = gtkIMContext;
97 		super(cast(GObject*)gtkIMContext, ownedRef);
98 	}
99 
100 
101 	/** */
102 	public static GType getType()
103 	{
104 		return gtk_im_context_get_type();
105 	}
106 
107 	/**
108 	 * Asks the widget that the input context is attached to delete
109 	 * characters around the cursor position by emitting the
110 	 * `::delete_surrounding` signal.
111 	 *
112 	 * Note that @offset and @n_chars are in characters not in bytes
113 	 * which differs from the usage other places in `GtkIMContext`.
114 	 *
115 	 * In order to use this function, you should first call
116 	 * [method@Gtk.IMContext.get_surrounding] to get the current context,
117 	 * and call this function immediately afterwards to make sure that you
118 	 * know what you are deleting. You should also account for the fact
119 	 * that even if the signal was handled, the input context might not
120 	 * have deleted all the characters that were requested to be deleted.
121 	 *
122 	 * This function is used by an input method that wants to make
123 	 * subsitutions in the existing text in response to new input.
124 	 * It is not useful for applications.
125 	 *
126 	 * Params:
127 	 *     offset = offset from cursor position in chars;
128 	 *         a negative value means start before the cursor.
129 	 *     nChars = number of characters to delete.
130 	 *
131 	 * Returns: %TRUE if the signal was handled.
132 	 */
133 	public bool deleteSurrounding(int offset, int nChars)
134 	{
135 		return gtk_im_context_delete_surrounding(gtkIMContext, offset, nChars) != 0;
136 	}
137 
138 	/**
139 	 * Allow an input method to forward key press and release events
140 	 * to another input method without necessarily having a `GdkEvent`
141 	 * available.
142 	 *
143 	 * Params:
144 	 *     press = whether to forward a key press or release event
145 	 *     surface = the surface the event is for
146 	 *     device = the device that the event is for
147 	 *     time = the timestamp for the event
148 	 *     keycode = the keycode for the event
149 	 *     state = modifier state for the event
150 	 *     group = the active keyboard group for the event
151 	 *
152 	 * Returns: %TRUE if the input method handled the key event.
153 	 */
154 	public bool filterKey(bool press, Surface surface, Device device, uint time, uint keycode, GdkModifierType state, int group)
155 	{
156 		return gtk_im_context_filter_key(gtkIMContext, press, (surface is null) ? null : surface.getSurfaceStruct(), (device is null) ? null : device.getDeviceStruct(), time, keycode, state, group) != 0;
157 	}
158 
159 	/**
160 	 * Allow an input method to internally handle key press and release
161 	 * events.
162 	 *
163 	 * If this function returns %TRUE, then no further processing
164 	 * should be done for this key event.
165 	 *
166 	 * Params:
167 	 *     event = the key event
168 	 *
169 	 * Returns: %TRUE if the input method handled the key event.
170 	 */
171 	public bool filterKeypress(Event event)
172 	{
173 		return gtk_im_context_filter_keypress(gtkIMContext, (event is null) ? null : event.getEventStruct()) != 0;
174 	}
175 
176 	/**
177 	 * Notify the input method that the widget to which this
178 	 * input context corresponds has gained focus.
179 	 *
180 	 * The input method may, for example, change the displayed
181 	 * feedback to reflect this change.
182 	 */
183 	public void focusIn()
184 	{
185 		gtk_im_context_focus_in(gtkIMContext);
186 	}
187 
188 	/**
189 	 * Notify the input method that the widget to which this
190 	 * input context corresponds has lost focus.
191 	 *
192 	 * The input method may, for example, change the displayed
193 	 * feedback or reset the contexts state to reflect this change.
194 	 */
195 	public void focusOut()
196 	{
197 		gtk_im_context_focus_out(gtkIMContext);
198 	}
199 
200 	/**
201 	 * Retrieve the current preedit string for the input context,
202 	 * and a list of attributes to apply to the string.
203 	 *
204 	 * This string should be displayed inserted at the insertion point.
205 	 *
206 	 * Params:
207 	 *     str = location to store the retrieved
208 	 *         string. The string retrieved must be freed with g_free().
209 	 *     attrs = location to store the retrieved
210 	 *         attribute list. When you are done with this list, you
211 	 *         must unreference it with [method@Pango.AttrList.unref].
212 	 *     cursorPos = location to store position of cursor
213 	 *         (in characters) within the preedit string.
214 	 */
215 	public void getPreeditString(out string str, out PgAttributeList attrs, out int cursorPos)
216 	{
217 		char* outstr = null;
218 		PangoAttrList* outattrs = null;
219 
220 		gtk_im_context_get_preedit_string(gtkIMContext, &outstr, &outattrs, &cursorPos);
221 
222 		str = Str.toString(outstr);
223 		attrs = ObjectG.getDObject!(PgAttributeList)(outattrs);
224 	}
225 
226 	/**
227 	 * Retrieves context around the insertion point.
228 	 *
229 	 * Input methods typically want context in order to constrain input text
230 	 * based on existing text; this is important for languages such as Thai
231 	 * where only some sequences of characters are allowed.
232 	 *
233 	 * This function is implemented by emitting the
234 	 * [signal@Gtk.IMContext::retrieve-surrounding] signal on the input method;
235 	 * in response to this signal, a widget should provide as much context as
236 	 * is available, up to an entire paragraph, by calling
237 	 * [method@Gtk.IMContext.set_surrounding].
238 	 *
239 	 * Note that there is no obligation for a widget to respond to the
240 	 * `::retrieve-surrounding` signal, so input methods must be prepared to
241 	 * function without context.
242 	 *
243 	 * Deprecated: Use [method@Gtk.IMContext.get_surrounding_with_selection] instead.
244 	 *
245 	 * Params:
246 	 *     text = location to store a UTF-8 encoded
247 	 *         string of text holding context around the insertion point.
248 	 *         If the function returns %TRUE, then you must free the result
249 	 *         stored in this location with g_free().
250 	 *     cursorIndex = location to store byte index of the insertion
251 	 *         cursor within @text.
252 	 *
253 	 * Returns: `TRUE` if surrounding text was provided; in this case
254 	 *     you must free the result stored in `text`.
255 	 */
256 	public bool getSurrounding(out string text, out int cursorIndex)
257 	{
258 		char* outtext = null;
259 
260 		auto __p = gtk_im_context_get_surrounding(gtkIMContext, &outtext, &cursorIndex) != 0;
261 
262 		text = Str.toString(outtext);
263 
264 		return __p;
265 	}
266 
267 	/**
268 	 * Retrieves context around the insertion point.
269 	 *
270 	 * Input methods typically want context in order to constrain input
271 	 * text based on existing text; this is important for languages such
272 	 * as Thai where only some sequences of characters are allowed.
273 	 *
274 	 * This function is implemented by emitting the
275 	 * [signal@Gtk.IMContext::retrieve-surrounding] signal on the input method;
276 	 * in response to this signal, a widget should provide as much context as
277 	 * is available, up to an entire paragraph, by calling
278 	 * [method@Gtk.IMContext.set_surrounding_with_selection].
279 	 *
280 	 * Note that there is no obligation for a widget to respond to the
281 	 * `::retrieve-surrounding` signal, so input methods must be prepared to
282 	 * function without context.
283 	 *
284 	 * Params:
285 	 *     text = location to store a UTF-8 encoded
286 	 *         string of text holding context around the insertion point.
287 	 *         If the function returns %TRUE, then you must free the result
288 	 *         stored in this location with g_free().
289 	 *     cursorIndex = location to store byte index of the insertion
290 	 *         cursor within @text.
291 	 *     anchorIndex = location to store byte index of the selection
292 	 *         bound within @text
293 	 *
294 	 * Returns: `TRUE` if surrounding text was provided; in this case
295 	 *     you must free the result stored in `text`.
296 	 *
297 	 * Since: 4.2
298 	 */
299 	public bool getSurroundingWithSelection(out string text, out int cursorIndex, out int anchorIndex)
300 	{
301 		char* outtext = null;
302 
303 		auto __p = gtk_im_context_get_surrounding_with_selection(gtkIMContext, &outtext, &cursorIndex, &anchorIndex) != 0;
304 
305 		text = Str.toString(outtext);
306 
307 		return __p;
308 	}
309 
310 	/**
311 	 * Notify the input method that a change such as a change in cursor
312 	 * position has been made.
313 	 *
314 	 * This will typically cause the input method to clear the preedit state.
315 	 */
316 	public void reset()
317 	{
318 		gtk_im_context_reset(gtkIMContext);
319 	}
320 
321 	/**
322 	 * Set the client widget for the input context.
323 	 *
324 	 * This is the `GtkWidget` holding the input focus. This widget is
325 	 * used in order to correctly position status windows, and may
326 	 * also be used for purposes internal to the input method.
327 	 *
328 	 * Params:
329 	 *     widget = the client widget. This may be %NULL to indicate
330 	 *         that the previous client widget no longer exists.
331 	 */
332 	public void setClientWidget(Widget widget)
333 	{
334 		gtk_im_context_set_client_widget(gtkIMContext, (widget is null) ? null : widget.getWidgetStruct());
335 	}
336 
337 	/**
338 	 * Notify the input method that a change in cursor
339 	 * position has been made.
340 	 *
341 	 * The location is relative to the client widget.
342 	 *
343 	 * Params:
344 	 *     area = new location
345 	 */
346 	public void setCursorLocation(GdkRectangle* area)
347 	{
348 		gtk_im_context_set_cursor_location(gtkIMContext, area);
349 	}
350 
351 	/**
352 	 * Sets surrounding context around the insertion point and preedit
353 	 * string.
354 	 *
355 	 * This function is expected to be called in response to the
356 	 * [signal@Gtk.IMContext::retrieve-surrounding] signal, and will
357 	 * likely have no effect if called at other times.
358 	 *
359 	 * Deprecated: Use [method@Gtk.IMContext.set_surrounding_with_selection] instead
360 	 *
361 	 * Params:
362 	 *     text = text surrounding the insertion point, as UTF-8.
363 	 *         the preedit string should not be included within @text
364 	 *     len = the length of @text, or -1 if @text is nul-terminated
365 	 *     cursorIndex = the byte index of the insertion cursor within @text.
366 	 */
367 	public void setSurrounding(string text, int len, int cursorIndex)
368 	{
369 		gtk_im_context_set_surrounding(gtkIMContext, Str.toStringz(text), len, cursorIndex);
370 	}
371 
372 	/**
373 	 * Sets surrounding context around the insertion point and preedit
374 	 * string. This function is expected to be called in response to the
375 	 * [signal@Gtk.IMContext::retrieve_surrounding] signal, and will likely
376 	 * have no effect if called at other times.
377 	 *
378 	 * Params:
379 	 *     text = text surrounding the insertion point, as UTF-8.
380 	 *         the preedit string should not be included within @text
381 	 *     len = the length of @text, or -1 if @text is nul-terminated
382 	 *     cursorIndex = the byte index of the insertion cursor within @text
383 	 *     anchorIndex = the byte index of the selection bound within @text
384 	 *
385 	 * Since: 4.2
386 	 */
387 	public void setSurroundingWithSelection(string text, int len, int cursorIndex, int anchorIndex)
388 	{
389 		gtk_im_context_set_surrounding_with_selection(gtkIMContext, Str.toStringz(text), len, cursorIndex, anchorIndex);
390 	}
391 
392 	/**
393 	 * Sets whether the IM context should use the preedit string
394 	 * to display feedback.
395 	 *
396 	 * If @use_preedit is %FALSE (default is %TRUE), then the IM context
397 	 * may use some other method to display feedback, such as displaying
398 	 * it in a child of the root window.
399 	 *
400 	 * Params:
401 	 *     usePreedit = whether the IM context should use the preedit string.
402 	 */
403 	public void setUsePreedit(bool usePreedit)
404 	{
405 		gtk_im_context_set_use_preedit(gtkIMContext, usePreedit);
406 	}
407 
408 	/**
409 	 * The ::commit signal is emitted when a complete input sequence
410 	 * has been entered by the user.
411 	 *
412 	 * If the commit comes after a preediting sequence, the
413 	 * ::commit signal is emitted after ::preedit-end.
414 	 *
415 	 * This can be a single character immediately after a key press or
416 	 * the final result of preediting.
417 	 *
418 	 * Params:
419 	 *     str = the completed character(s) entered by the user
420 	 */
421 	gulong addOnCommit(void delegate(string, IMContext) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
422 	{
423 		return Signals.connect(this, "commit", dlg, connectFlags ^ ConnectFlags.SWAPPED);
424 	}
425 
426 	/**
427 	 * The ::delete-surrounding signal is emitted when the input method
428 	 * needs to delete all or part of the context surrounding the cursor.
429 	 *
430 	 * Params:
431 	 *     offset = the character offset from the cursor position of the text
432 	 *         to be deleted. A negative value indicates a position before
433 	 *         the cursor.
434 	 *     nChars = the number of characters to be deleted
435 	 *
436 	 * Returns: %TRUE if the signal was handled.
437 	 */
438 	gulong addOnDeleteSurrounding(bool delegate(int, int, IMContext) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
439 	{
440 		return Signals.connect(this, "delete-surrounding", dlg, connectFlags ^ ConnectFlags.SWAPPED);
441 	}
442 
443 	/**
444 	 * The ::preedit-changed signal is emitted whenever the preedit sequence
445 	 * currently being entered has changed.
446 	 *
447 	 * It is also emitted at the end of a preedit sequence, in which case
448 	 * [method@Gtk.IMContext.get_preedit_string] returns the empty string.
449 	 */
450 	gulong addOnPreeditChanged(void delegate(IMContext) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
451 	{
452 		return Signals.connect(this, "preedit-changed", dlg, connectFlags ^ ConnectFlags.SWAPPED);
453 	}
454 
455 	/**
456 	 * The ::preedit-end signal is emitted when a preediting sequence
457 	 * has been completed or canceled.
458 	 */
459 	gulong addOnPreeditEnd(void delegate(IMContext) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
460 	{
461 		return Signals.connect(this, "preedit-end", dlg, connectFlags ^ ConnectFlags.SWAPPED);
462 	}
463 
464 	/**
465 	 * The ::preedit-start signal is emitted when a new preediting sequence
466 	 * starts.
467 	 */
468 	gulong addOnPreeditStart(void delegate(IMContext) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
469 	{
470 		return Signals.connect(this, "preedit-start", dlg, connectFlags ^ ConnectFlags.SWAPPED);
471 	}
472 
473 	/**
474 	 * The ::retrieve-surrounding signal is emitted when the input method
475 	 * requires the context surrounding the cursor.
476 	 *
477 	 * The callback should set the input method surrounding context by
478 	 * calling the [method@Gtk.IMContext.set_surrounding] method.
479 	 *
480 	 * Returns: %TRUE if the signal was handled.
481 	 */
482 	gulong addOnRetrieveSurrounding(bool delegate(IMContext) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
483 	{
484 		return Signals.connect(this, "retrieve-surrounding", dlg, connectFlags ^ ConnectFlags.SWAPPED);
485 	}
486 }